<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/28 14:19
 */

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\User;
use App\Http\Models\RechargeLog;
use App\Http\Models\UserCapital;
use App\Http\Models\VoucherLog;

class FinanceController extends Controller
{
    use ModelForm;
    protected $headerStr = '点券';

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->description('用户点券');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Index interface.
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->headerStr . '管理');
            //$content->description($this->headerStr . '列表...');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->headerStr . '管理');
            $content->description('编辑' . $this->headerStr);
            $content->body($this->form()->edit($id));
        });
    }


    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->headerStr . '管理');
            //$content->description('新增' . $this->headerStr);
            $content->row(function ($row) {
                $row->column(6, $this->sreach_grid());
                $row->column(6, $this->form());
            });
        });
    }


    protected function grid()
    {
        return Admin::grid(VoucherLog::class, function (Grid $grid) {

            $grid->model()->select('users_voucher_log.id', 'users.name', 'users.avatar', 'users.phone', 'users_voucher_log.created_at'
                , 'users_voucher_log.content', 'users_voucher_log.status', 'users_voucher_log.number', 'users_voucher_log.balance', 'users_voucher_log.user_id'
            )->leftjoin('users', 'users.id', '=', 'users_voucher_log.user_id')->orderBy('id', 'desc');
            $grid->id('编号');
            $grid->user_id('用户ID(昵称)')->display(function () {
                return $this->user_id . '(' . $this->name . ')';
            })->sortable();
            $grid->avatar('头像')->display(function ($avatar) {
                return user_image_url($avatar);
            })->image(40, 40);
            $grid->phone('手机号');
            $grid->number('数量')->display(function ($number) {
                $s = (in_array($this->status, [2, 1])) ? '+ ' : '- ';
                return $s . $number;
            });
            $grid->balance('余额')->sortable();
            $grid->created_at('时间')->sortable();
            $grid->status('类型')->display(function ($value) {
                switch ((int)$value) {  //0发布商品,1商品被拒,2充值,3管理员扣款
                    case 2:
                        $str = '<span class=" label label-success">充值</span>';
                        break;
                    case 1:
                        $str = '<span class=" label label-warning">商品被拒返回</span>';
                        break;
                    case 3:
                        $str = '<span class=" label label-info">管理员扣款</span>';
                        break;
                    default:
                        $str = '<span class=" label label-primary">发布商品</span>';
                }
                return $str;
            });
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                $filter->equal('status', '类型')->select(['2' => '充值', '0' => '发布商品', '1' => '商品被拒返回', '3' => '管理员扣款']);
                $filter->like('name', '用户昵称');
                $filter->equal('user_id', '用户ID');
                $filter->equal('phone', '用户手机号');
            });
            //$grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->paginate(env('Admin_Limit'));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(RechargeLog::class, function (Form $form) {
            $form->text('user_id', '用户id:');
            $form->number('money', '优惠券总量:')->max(100000000)->rules('required|regex:/^\d+$/|min:2', [
                'regex' => 'code必须全部为数字',
                'min' => 'code不能少于2个字符',
            ]);
            $form->disableReset();
            $form->radio('status', '充值/扣款:')->options(['1' => '充值', '0' => '扣款']);
            $form->text('content', '描述:')->placeholder('请说明充值或扣款原因')->rules('required|min:3');
            // $form->setWidth(3, 2);
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                //$tools->disableView(); // 去掉跳转列表按钮
            });
            $form->saving(function (Form $form) {
                $staus = $form->status == 1 ? 2 : 3;
                //$content = ($staus == 2) ? '管理员充值' . request('money') . '点券,原因' . request('content') : '管理员扣款' . request('money') . '点券,原因' . request('content');
                UserCapital::addUserCapital(request('user_id'), request('money'), $staus, request('content'));
            });

        });
    }

    protected function sreach_grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->id('用户ID')->sortable();
            $grid->name('昵称');
            $grid->avatar('头像')->image(40, 40);
            $grid->phone('手机号');
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->filter(function ($filter) {
                //$filter->disableIdFilter();//禁用查询过滤器
                $filter->like('name', '用户昵称');
                $filter->like('phone', '手机号');
            });
            $grid->expandFilter();
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                //$actions->disableView();
                $actions->disableEdit();
            });
            $grid->disableActions();
            $grid->paginate(env('Admin_Limit'));
        });
    }
}